const fs = require("fs");
const Koa = require("koa");
const KoaRouter = require("@koa/router");

const app = new Koa();

const userRouter = new KoaRouter({ prefix: "/users" });

userRouter.get("/", (ctx, next) => {
  // 1. body 的类型是string
  // ctx.body = 'user list data~'

  // 2. body 的类型是Buffer
  // ctx.body = Buffer.from('user list data~')

  // 3. body 的类型是Stream
  // const readStream = fs.createReadStream('./uploads/1730873911828-sbem.jpg')
  // ctx.type = 'image/jpeg'
  // ctx.body = readStream

  // 4. body 的类型是Array/Object(使用最多)
  ctx.status = 201;
  ctx.body = {
    code: 0,
    data: [
      { id: 111, name: "iphone 14", price: 100 },
      { id: 112, name: "iphone 15", price: 200 },
    ],
  };

  // 5.body的值是null, 自动设置http status code为204
  // ctx.body = null
});

app.use(userRouter.routes());
app.use(userRouter.allowedMethods());

app.listen(6000, () => {
  console.log("server start~");
});
